﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using Tenancy.Helper;
using System.Data;
using SQLServer.DBUtility;
namespace Tenancy.Server
{
    /// <summary>
    /// Hbung_OrderInfo 的摘要说明
    /// </summary>
    public class Hbung_OrderInfo : IHttpHandler
    {
        /// <summary>
        /// 订单详情接口
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //用户登录的手机号
            string Hbung_Tel = context.Request.Params["Tel"];
            string Hbung_Phone = string.Empty;
            if (string.IsNullOrEmpty(Hbung_Tel))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Tel为空"));
            }
            else
            {
                string pattern = "^1[0-9]{10}$"; //正则表达式字符串
                Regex cgsregex = new Regex(pattern);
                Hbung_Phone = DESEncrypt.MD5Decrypt(Hbung_Tel);
                bool isMatch = cgsregex.IsMatch(Hbung_Phone);
                if (!isMatch)
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Tel格式不正确"));
                    return;
                }
            }
            //签名
            string HBsign = context.Request.Params["Sign"];
            if (string.IsNullOrEmpty(HBsign))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Sign为空"));
                return;
            }
            else
            {
                if (GenerClass.IsOkMatch(Hbung_Phone) != Regex.Replace(HBsign, @"\s", "+"))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Sign不匹配"));
                    return;
                }
            }

            //订单号
            string Hbung_OrderNum = context.Request.Params["OrderNum"];
            if (string.IsNullOrEmpty(Hbung_OrderNum))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("OrderNum不能为空"));
                return;
            }

            //商品id
            string Hbung_SPID = context.Request.Params["SPID"];
            if (string.IsNullOrEmpty(Hbung_SPID))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("SPID不能为空"));
                return;
            }

            string H_Order = string.Format(@"select ID,OrderNum,ProductType,PBrands,PMachineModel,RentStartDate,RentEndDate,RentWay,RentUnitPrice,RentTotalPrice,MachineUseAddress,
               TransportType,FreightToAuto,ScheduledUseTime,SLinkman,SLinkPhone,Deposit,OperatorCost,ExecutoryCost,RefuelCost,TotalPrice,OrderStatus,PaymentMethod,
               ( DATEDIFF(DAY,RentStartDate,RentEndDate)+1 ) as Days,DeviceDID,PeoType,SalemanName,SalemanRegion,SalemanTel,FavorableForOnline
                   from OnlineOrders where OrderNum='{0}' and DeviceDID='{1}' ", Hbung_OrderNum, Hbung_SPID);
            DataTable MyOrder=DbLocalSQL.Query(H_Order).Tables[0];
            if(MyOrder!=null&&MyOrder.Rows.Count>0)
            {

                string Hb_DateJson = JsonHelper.DataTableToJSON(MyOrder);
                context.Response.Write(GenerClass.SetSucessJsData("查询成功", Hb_DateJson));
            }
            else
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("订单不存在"));
                return;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}